/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hello;

import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import java.util.*;

/**
 *
 * @author HOME
 */
public class Bomb extends Sprite{
    static final int WIDTH = 16;
    static final int HEIGHT = 16;
    static final int FireLen = 2;
    
    public int myInitialX;
    public int myInitialY;
    static Image Image1;
    static Image Image2;
    static Image TamNo1;
       
    boolean BomNo = false;
    boolean TaoTiaLua = false;
    //int a[] = {1,2,3};
    
    NhanhLua[] Phai;
    NhanhLua[] Trai;
    NhanhLua[] Tren;
    NhanhLua[] Duoi;
    
    NhanhLua[][] Nhanh ;
    private Clock clock;
    int LengTrai;
    int LengPhai;
    int LengTren;
    int LengDuoi;
    public Bomb(int x, int y)throws Exception
    {

        super(Image.createImage("/images/BomberBombs/1.gif"), 
	  WIDTH, HEIGHT);
        myInitialX = x;
        myInitialY = y;
        
        defineReferencePixel(WIDTH/2, HEIGHT/2);
        setRefPixelPosition(myInitialX, myInitialY);
        
        Image1 = Image.createImage("/images/BomberBombs/1.gif");
        Image2 = Image.createImage("/images/BomberBombs/2.gif");
        
        clock = new Clock(5);
        new Timer().schedule(clock, 0, 1000);
    }
    
    private boolean Hinh1 = true;
   
    int XacDinhPhamVi(int direction, int CuongDo)
    {
        return CuongDo;
    }
    
    void ThemNhanhPhai(JumpManager myManager)
    {
        try {
            int i;
            LengPhai =0;
            for(i = 0; i < Phai.length; i++)
            {
                Phai[i] = new NhanhLua(myInitialX + WIDTH*(i+1), myInitialY, TRANS_NONE);
                myManager.insert(Phai[i], myManager.getSize()-1);
                if(KiemTraDungCham(Phai[i], myManager) ==true)
                {
                    
                    break;
                }
                LengPhai ++;
            }
        } catch (Exception exception) {
        }     
    }
    void ThemNhanhTrai(JumpManager myManager)
    {
        try {
            int i;
            LengTrai =0;
            for(i = 0; i < Trai.length; i++)
            {
                Trai[i] = new NhanhLua(myInitialX - WIDTH*(i+1), myInitialY , TRANS_NONE);
                myManager.insert(Trai[i], myManager.getSize()-1);
                if(KiemTraDungCham(Trai[i], myManager) ==true)
                {
                    
                    break;
                }
                LengTrai ++;
            }
        } catch (Exception exception) {
        }     
    }
    void ThemNhanhTren(JumpManager myManager)
    {
        try {
            int i;
            LengTren = 0;
            for(i = 0; i < Tren.length; i++)
            {
                
                Tren[i] = new NhanhLua(myInitialX, myInitialY + HEIGHT*(i+1), TRANS_ROT90);
                myManager.insert(Tren[i], myManager.getSize()-1);
                if(KiemTraDungCham(Tren[i], myManager) ==true)
                {
                    
                    break;
                }
                LengTren++;
            }
        } catch (Exception exception) {
        }     
    }
    void ThemNhanhDuoi(JumpManager myManager)
    {
        try {
            int i;
            LengDuoi =0;
            for(i = 0; i < Duoi.length; i++)
            {
                
                Duoi[i] = new NhanhLua(myInitialX , myInitialY - HEIGHT*(i+1), TRANS_ROT90);
                myManager.insert(Duoi[i], myManager.getSize()-1);
                if(KiemTraDungCham(Duoi[i], myManager) ==true)
                {
                    
                    break;
                }
                LengDuoi ++;
            }
        } catch (Exception exception) {
        }     
    }
    void advanceNhanh (NhanhLua a[])
    {
        for(int i = 0; i< a.length; i++)
        {
            if (a[i] != null)
                a[i].andvance();
        }
    }
    int num = 2;
    static String path = "/images/BomberFires/C";
    Image tam;
    
    void RemoveNhanh(LayerManager JumManager)
    {
        int i;
        /*
        for(i = 0; i< Phai.length; i++)
        {
            JumManager.remove(Phai[i]);
        }
        for(i = 0; i< Trai.length; i++)
        {
            JumManager.remove(Trai[i]);
        }
        for(i = 0; i< Tren.length; i++)
        {
            JumManager.remove(Tren[i]);
        }*/
        
        for(i = 0; i< Duoi.length; i++)
        {
            if (Phai[i] !=null)
                JumManager.remove(Phai[i]);
            if (Trai[i] !=null)
                JumManager.remove(Trai[i]);
            if (Duoi[i] != null)
                JumManager.remove(Duoi[i]);
            if (Tren[i] != null)
                JumManager.remove(Tren[i]);
        }
        
    }
    boolean KiemTraDungCham(NhanhLua lua,JumpManager myManager)
    {
        boolean vacham = false;
        int row,colum;    
        //Neu Dung Tuong
        if (lua.collidesWith(myManager.background,false) ==true)
        {
            vacham = true;
            row = lua.getY()/myManager.Square;
            colum = (lua.getX()+10)/myManager.Square;
            if (myManager.cells[row][colum] >1)
            {
                if (myManager.cells[row][colum]==2)
                    myManager.background.setCell(colum, row, 0);
                else
                {
                    myManager.background.setCell(colum, row, myManager.cells[row][colum]);
                  myManager.cellsClone[row][colum] = myManager.cells[row][colum];
                  myManager.cells[row][colum] = 0;
                }
            }
        }
        //Neu Dung Quai Vat
        int i;
        for (i = 0 ; i < myManager._ListMonster.length; i ++)
        {
            if (lua.collidesWith(myManager._ListMonster[i],true))
            {
                myManager._ListMonster[i].setVisible(false);
                vacham = true;
            }
        }
        if (lua.collidesWith(myManager.myPlayer, true))
        {
           myManager.myPlayer.IsAlive = false;
           vacham = true;
        }
        
        return vacham;
    }
    void advance(int CuongDo, JumpManager myManager) {
        
        if(clock.timeLeft == 0)
        {
            try {
                tam = Image.createImage(path + String.valueOf(1)+".gif");
            } catch (Exception exception) {
            }
             clock.cancel();
            setImage(tam, WIDTH, WIDTH);
            setPosition(myInitialX, myInitialY);
            
            if(TaoTiaLua == false)
            {
                //Nhanh = new NhanhLua[4][CuongDo];
                //Phat tia lua ben phai
                int PhamVi = XacDinhPhamVi(1, CuongDo);
                Phai = new NhanhLua[PhamVi-1];
                ThemNhanhPhai(myManager);
                
                //PhamVi = XacDinhPhamVi(2, CuongDo);
                Trai = new NhanhLua[PhamVi-1];
                ThemNhanhTrai(myManager);
                
                //PhamVi = XacDinhPhamVi(3, CuongDo);
                Tren = new NhanhLua[PhamVi-1];
                ThemNhanhTren(myManager);
                
                //PhamVi = XacDinhPhamVi(4, CuongDo);
                Duoi = new NhanhLua[PhamVi-1];
                ThemNhanhDuoi(myManager);
                
                TaoTiaLua = true;
               
            }
            else
            {
                advanceNhanh(Phai);
                advanceNhanh(Trai);
                advanceNhanh(Tren);
                advanceNhanh(Duoi);
                
//                try {
//                tam = Image.createImage(path + String.valueOf(num)+".gif");
//                } catch (Exception exception) {
//                }
//
//                setImage(tam, WIDTH, WIDTH);
//                setPosition(myInitialX, myInitialY);
//                num = num + 2;
              //  if(Phai[0].num > 3)
                    BomNo = true;
            }
            
        }
        else
        {
            if(Hinh1 == true)
            {
                setImage(Image1,WIDTH , HEIGHT);
                setPosition(myInitialX, myInitialY);
                Hinh1 = false;
            }
            else
            {
                setImage(Image2,WIDTH , HEIGHT);
                setPosition(myInitialX, myInitialY);
                Hinh1 = true;
            }
            
        }
    }
    
}
